package com.ciwong.epaper.modules.me.ui.mall;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.Service;
import com.ciwong.epaper.util.MathExtend;
import com.ciwong.mobilelib.bean.BaseBean;

/**
 * Created by neo on 2016/7/5.
 * Modify by young on 2016/08//24
 * 价格列表控件
 */
public class ItemServiceInfo extends LinearLayout{

    private ImageView iv_service_logo;
    private TextView tv_service_name;
    private  TextView price_now_sale;
    private TextView     tv_on_sale_remain_time;
    private  TextView tv_discoint_sale;
    private  TextView money_singn1_sale;
    private  TextView tv_buy_count;
    private RecyclerView recyclerView;
    private SpannableString msp = null;
    private ItemPriceAdapter itemPriceAdapter;
    private Context mContext;
    private PriceBinder priceBinder;

    public interface PriceBinder{
        public void price(float nowPrice,float OriginalPrice,int mounth);
    }

    public void setPriceBinder(PriceBinder priceBinder){
        this.priceBinder=priceBinder;
    }
    public ItemServiceInfo(Context context) {
        super(context);
        mContext=context;
        LayoutInflater.from(this.getContext()).inflate(R.layout.item_service_info, this);
        initView( );
    }

    public ItemServiceInfo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
        LayoutInflater.from(this.getContext()).inflate(R.layout.item_service_info, this);
        initView( );
    }

    public ItemServiceInfo(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        LayoutInflater.from(this.getContext()).inflate(R.layout.item_service_info, this);
        initView( );
    }

    private void initView() {
        tv_buy_count = (TextView) findViewById(R.id.tv_buy_count);
        recyclerView=(RecyclerView)findViewById(R.id.price_recycerview);
        itemPriceAdapter=new ItemPriceAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(itemPriceAdapter);
    }

    public void update(Service service) {
        try{
            if (service != null) {
                if(service.getSign()==3){
                    tv_buy_count.setText(service.getBuycount() + "人已获取");
                }else {
                    tv_buy_count.setText(service.getBuycount() + "人已购买");
                }
                itemPriceAdapter.setService(service);
            }
        }catch (Exception e){
            e.getStackTrace();
        }
    }

    /**
     * 价格列表适配器
     */
    public class  ItemPriceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        private Service service;
        private Boolean[] booleen;
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=View.inflate(parent.getContext(),R.layout.item_price,null);
            PriceViewHolder priceViewHolder=new PriceViewHolder(view);
            return priceViewHolder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            java.text.DecimalFormat   df   =new   java.text.DecimalFormat("#0.00");
            if(null!=service){
                //买断
                PriceViewHolder priceViewHolder=(PriceViewHolder)holder;
                if(service.getSign()==1||service.getSign()==3){
                    if(service.ispromo()){
                        priceViewHolder.imageView.setChecked(true);
                        //买断促销情况下
                        String price="￥"+df.format(service.getPromotion().getPrice().get(position).getOprice())+"";
                        SpannableString spannableString=new SpannableString("("+price+")");
                        spannableString.setSpan(new StrikethroughSpan(),0,spannableString.length(),Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                        String sale=df.format(service.getPromotion().getPrice().get(position).getDprice())+"元";
                        priceViewHolder.money_sale.setText(sale);
                        priceViewHolder.zheKe.setText(spannableString);
                        double oPrice=new Double(service.getPromotion().getPrice().get(position).getDprice());
                        double discount=0;

                        if(oPrice>0) {
                            discount = Double.valueOf(service.getPromotion().getPrice().get(position).getDprice())/Double.valueOf(service.getPromotion().getPrice().get(position).getOprice());
                            discount=discount*10;
                        }
                        priceViewHolder.discount.setText(new java.text.DecimalFormat("#0.0").format(discount)+"折");
                        priceBinder.price(service.getPromotion().getPrice().get(position).getDprice(),service.getPromotion().getPrice().get(position).getOprice(),service.getPromotion().getPrice().get(position).getMonth());
                    }else {
                        priceViewHolder.imageView.setChecked(true);
                        String price=df.format(service.getPrice().get(position).getPrice())+"";
                        priceViewHolder.money_sale.setText(price+"元");
                        //隐藏折扣
                        priceViewHolder.discount.setText("");
                        priceBinder.price(service.getPrice().get(position).getPrice(),service.getPrice().get(position).getPrice(),service.getPrice().get(position).getUnit());
                    }



                } else {

                    if(service.ispromo()){
                        String price="￥"+df.format(service.getPromotion().getPrice().get(position).getOprice())+"";
                        SpannableString spannableString=new SpannableString("("+price+")");
                        spannableString.setSpan(new StrikethroughSpan(),0,spannableString.length(),Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                        String sale=service.getPromotion().getPrice().get(position).getMonth()+"个月/"+df.format(service.getPromotion().getPrice().get(position).getDprice())+"元";
                        priceViewHolder.money_sale.setText(sale);
                        priceViewHolder.zheKe.setText(spannableString);
                        double oPrice=new Double(service.getPromotion().getPrice().get(position).getDprice());
                        double discount=0;
                        if(oPrice>0) {
                            discount = Double.valueOf(service.getPromotion().getPrice().get(position).getDprice())/Double.valueOf(service.getPromotion().getPrice().get(position).getOprice());
                            discount=discount*10;
                        }
                        priceViewHolder.discount.setText(new java.text.DecimalFormat("#0.0").format(discount)+"折");
                        if(null!=booleen&&booleen[position]){
                            priceViewHolder.imageView.setChecked(true);
                            priceBinder.price(service.getPromotion().getPrice().get(position).getDprice(),service.getPromotion().getPrice().get(position).getOprice(),service.getPromotion().getPrice().get(position).getMonth());
                        }else {
                            priceViewHolder.imageView.setChecked(false);
                        }
                        //选择价格
                        priceViewHolder.item_service.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                for(int i=0;i<booleen.length;i++){
                                    booleen[i]=false;
                                    if(i==position){
                                        booleen[i]=true;
                                    }
                                }
                                notifyDataSetChanged();
                                priceBinder.price(service.getPromotion().getPrice().get(position).getDprice(),service.getPromotion().getPrice().get(position).getOprice(),service.getPromotion().getPrice().get(position).getMonth());
                            }
                        });
                        priceViewHolder.imageView.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                for(int i=0;i<booleen.length;i++){
                                    booleen[i]=false;
                                    if(i==position){
                                        booleen[i]=true;
                                    }
                                }
                                notifyDataSetChanged();
                                priceBinder.price(service.getPromotion().getPrice().get(position).getDprice(),service.getPromotion().getPrice().get(position).getOprice(),service.getPromotion().getPrice().get(position).getMonth());
                            }
                        });

                    }else {

                        String price=df.format(service.getPrice().get(position).getPrice())+"";
                        String sale=service.getPrice().get(position).getUnit()+"个月/"+price+"元";
                        priceViewHolder.money_sale.setText(sale);
                        priceViewHolder.discount.setText("");
                        if(null!=booleen&&booleen[position]){
                            priceViewHolder.imageView.setChecked(true);
                            priceBinder.price(service.getPrice().get(position).getPrice(),service.getPrice().get(position).getPrice(),service.getPrice().get(position).getUnit());
                        }else {
                            priceViewHolder.imageView.setChecked(false);
                        }

                        //选择价格
                        priceViewHolder.item_service.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                for(int i=0;i<booleen.length;i++){
                                    booleen[i]=false;
                                    if(i==position){
                                        booleen[i]=true;
                                    }
                                }
                                notifyDataSetChanged();
                                priceBinder.price(service.getPrice().get(position).getPrice(),service.getPrice().get(position).getPrice(),service.getPrice().get(position).getUnit());
                            }
                        });
                        priceViewHolder.imageView.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                for(int i=0;i<booleen.length;i++){
                                    booleen[i]=false;
                                    if(i==position){
                                        booleen[i]=true;
                                    }
                                }
                                notifyDataSetChanged();
                                priceBinder.price(service.getPrice().get(position).getPrice(),service.getPrice().get(position).getPrice(),service.getPrice().get(position).getUnit());
                            }
                        });

                    }
                }
            }

        }

        @Override
        public int getItemCount() {
            if(null!=service){
                //是否促销
                if(service.ispromo()){
                    //是否买断
                    if(service.getSign()==1){
                        return 1;
                    }else {
                        return service.getPromotion().getPrice().size();
                    }
                }else {
                    //是否买断
                    if(service.getSign()==1){
                        return 1;
                    }else {
                        return  service.getPrice().size();
                    }
                }
            }
            return 0;
        }

        @Override
        public int getItemViewType(int position) {
          return position;
        }
        public void setService(Service service){
            if(null!=service){
                //买断
                if(service.getSign()==1){
                    if(service.ispromo()){
                        booleen=new Boolean[1];
                        booleen[0]=true;
                    }else {
                        booleen=new Boolean[1];
                        booleen[0]=true;
                    }

                }else {
                    if(service.ispromo()){
                        booleen=new Boolean[service.getPromotion().getPrice().size()];
                        for(int i=0;i<service.getPromotion().getPrice().size();i++){
                            booleen[i]=false;
                            if(i==0){
                                booleen[0]=true;
                            }
                        }
                    }else {
                        booleen=new Boolean[service.getPrice().size()];
                        for(int i=0;i<service.getPrice().size();i++){
                            booleen[i]=false;
                            if(i==0){
                                booleen[0]=true;
                            }
                        }
                    }
                }
                this.service=service;
                notifyDataSetChanged();
            }
        }
    }

    public class PriceViewHolder extends RecyclerView.ViewHolder{
        public CheckBox imageView;
        public TextView money_sale;
        public TextView discount;
        public View item_service;
        public TextView zheKe; //折扣


        public PriceViewHolder(View itemView) {
            super(itemView);
            imageView= (CheckBox)itemView.findViewById(R.id.select_button);
            money_sale=(TextView)itemView.findViewById(R.id.money_singn_sale);
            discount= (TextView) itemView.findViewById(R.id.tv_discoint_sale);
            item_service=itemView.findViewById(R.id.item_service);
            zheKe=(TextView)itemView.findViewById(R.id.pars_money);
        }


    }
}
